let select = document.querySelector("select");
let option = document.querySelector("option");
let ul = document.querySelector(".session ul");

_ajax({
	url: apiUrl.chooselist,
	method: "GET",
	data: {
		token: sessionStorage.getItem('token')
	}
}).then((res) => {
	let val = JSON.parse(res);
	val.result.list.forEach((item) => {
		select.innerHTML += `<option>${item.day_time}</option>`
	})
	// 建立新字段是setItem,获取用getItem
}).then((res) => {
	showlist();
}).catch(err => {
	console.log("请输入完整的信息");
})

select.addEventListener("change", () => {
	ul.innerHTML = "";
	showlist();
})


function showlist() {
	let daytime = select.children[select.selectedIndex].innerText;
	console.log(daytime)
	_ajax({
		url: apiUrl.chooseday,
		method: "GET",
		data: {
			token: sessionStorage.getItem('token'),
			day_time: daytime,
		},
		
	}).then((res) => {
		let val = JSON.parse(res);
		let timelist = val.result.list;
		timelist.forEach((item, index) => {
			ul.innerHTML +=
				`
				<li>
				<p>${item.show_name}</p>
				<span>可预约</span>
				</li>
				`
		})
		let lis = document.querySelectorAll("ul li")
		lis.forEach((item, index) => {
			item.addEventListener("click", () => {
				let showid = timelist[index].show_id
				// 预约接口返回的参数里面有个id参数是请求二维码需要用到的
				_ajax({
					url: apiUrl.bookday,
					method: "POST",
					data: {
						token: sessionStorage.getItem('token'),
						day_time: daytime,
						show_id: showid
					},
				}).then((res) => {
					let val = JSON.parse(res);
					if (val.msg == "操作成功") {
						sessionStorage.setItem("qrId", val.result.id);
						sessionStorage.setItem("showname", timelist[index].show_name);
						sessionStorage.setItem("daytime", daytime);
						console.log(val.result);
					}
					window.location.href = "success.html";
				})
			})
		})
	})
}
